package com.example.dmgame.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.dmgame.R;
import com.example.dmgame.bean.ChapterContent;
import com.example.dmgame.utils.JsonAsyncChapterCommentTask;
import com.example.dmgame.utils.JsonAsyncChapterCommentTask.ChapterContentCallback;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class ChapterContentActivity extends Activity implements OnClickListener {
	private TextView title, senddate,commentBtn;
	WebView webView;
	Button returnBtn;
	String id;
	int typeid;
	String arcurl;
	

	ProgressDialog dialog;

	public ChapterContentActivity() {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chaptercontent);
		dialog = new ProgressDialog(ChapterContentActivity.this);
		dialog.setTitle("Loading");
		dialog.setMessage("Please wait loading...");
		dialog.show();
		

		title = (TextView) this.findViewById(R.id.chaptercontentTitle);
		senddate = (TextView) this.findViewById(R.id.chaptercontentSenddate);
		webView = (WebView) this.findViewById(R.id.webView1);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		id = bundle.getString("id");
		typeid = bundle.getInt("typeid");

		Log.i("ChapterContentActivity", "---->>id=" + id + ",typeid=" + typeid);

		loadChapterComment();

		returnBtn = (Button) this.findViewById(R.id.returnBtn1);
		returnBtn.setOnClickListener(this);
		
		commentBtn = (TextView)this.findViewById(R.id.commentBtn);
		commentBtn.setOnClickListener(this);
		
		// Log.i("ChapterContentActivity", "---->>arcurl="+arcurl);

	}

	private void loadChapterComment() {
		JsonAsyncChapterCommentTask task = new JsonAsyncChapterCommentTask(
				new ChapterContentCallback() {

					@Override
					public void sendData(ChapterContent chapterContent) {
						// TODO Auto-generated method stub
						Log.i("ChapterContentActivity", "---->>chapterContent="
								+ chapterContent);
						String titleTxt = chapterContent.getTitle();
						String senddateTxt = chapterContent.getSenddate();
						String litpicTxt = chapterContent.getLitpic();
						String bodyTxt = null;
						try {
							bodyTxt = URLDecoder.decode(
									chapterContent.getBody(), "utf-8");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						title.setText(titleTxt);
						SimpleDateFormat format = new SimpleDateFormat(
								"yyyy年MM月dd日");
						senddate.setText(format.format(new Date(Long
								.parseLong(senddateTxt))));
						webView.loadDataWithBaseURL("http://www.3dmgame.com/",
								bodyTxt, "text/html", "UTF-8", null);

					}
				});
		task.execute(loadChapterContentURL(id, typeid));
	}

	@Override
	public void onBackPressed() {
		if (webView.canGoBack()) {
			webView.goBack();
		} else {
			finish();
		}
	}

	private String loadChapterContentURL(String id, int typeid) {
		return "http://www.3dmgame.com/sitemap/api.php?id=" + id + "&typeid="
				+ typeid;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBtn1:
			finish();
			break;
		case R.id.commentBtn:
			Intent intent = new Intent(ChapterContentActivity.this, CommentActivity.class);
			intent.putExtra("aid", id);
			startActivity(intent);
		default:
			break;
		}
	}

}
